/**
 * Created by elead on 2019/1/2.
 */
function typePOLFn(addFlage,type,arrAdd){
    if(addFlage!=undefined){
        var nameArr=arrAdd?arrAdd:"";
        var classType=type?type:"";
    }else{
        var nameArr=getQueryStringByNameTwo("nameArr")?getQueryStringByNameTwo("nameArr"):"";
        nameArr=decodeURIComponent(nameArr);
        var classType=getQueryStringByNameTwo("checkVal")?getQueryStringByNameTwo("checkVal"):"";
    }

    var getLocalStorage=localStorage.getItem("commonClassTypePOL")?localStorage.getItem("commonClassTypePOL"):"";
    getLocalStorage=getLocalStorage.split("&;");
    if(getLocalStorage.length>0){
        //window.localStorage.removeItem("commonClassType")
    }

    createLeftTab();
    function createLeftTab(){
        nameArr=nameArr.replace(/[\ |\[|\]]/g,"");
        nameArr=nameArr.split(",");
        if(classType=="POL"&&(getIndexof(nameArr,"虚拟偏光片组件")>(-1))){
            $(".tabRight").css({
                "margin-left":"160px"
            })
        }else{
            $(".tabRight").css({
                "margin-left":"120px"
            })
        }
        var leftTable="";
        for(var i=0;i<nameArr.length;i++){
            leftTable+='<tr><td><input type="checkbox"/></td><td>'+nameArr[i]+'</td></tr>'
        }
        $(".tabLeft").append(leftTable);
    }
    createRightTab();
    function createRightTab(){
        var publicClass=commonObj.POLPublicAttName;//列
        var publicText=commonObj.POLEditAttName;
        var newPubArr=[];
        for(var j=0;j<publicText.length;j++){
            newPubArr.push(publicText[j])
        }
        newPubArr.push.apply(newPubArr,publicClass);
        if(addFlage==undefined||addFlage==""){
        var Title="<thead><tr><td title='物料编号'><span>物料编号</span></td><td title='名称'><span>名称</span></td>";
        for(var i=0;i<newPubArr.length;i++){
           // Title+="<td title='"+newPubArr[i]+"'><span>"+newPubArr[i]+"</span></td>";
            if(newPubArr[i]=="默认单位"||newPubArr[i]=="源"){
                Title+="<td title='"+newPubArr[i]+"'><span><i style='color:red'>*</i>"+newPubArr[i]+"</span></td>";
            }else{
                Title+="<td title='"+newPubArr[i]+"'><span>"+newPubArr[i]+"</span></td>";
            }
        }
        Title+="</tr></thead>";
        }
       // if(addFlage==undefined||addFlage==""){
        $(".tabRight").append(Title);
        var tableTd="<tbody>"
        for(var n=0;n<nameArr.length;n++){
            if(addFlage==undefined||addFlage==""){
                tableTd+="<tr><td><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td><td><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td>";
            }else{
                tableTd+="<tr class='addFlage"+arrAdd+"'><td><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td><td><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td>";
            }

            for(var m=0;m<newPubArr.length;m++){
                if(m==0||m==2){
                    if(m==0){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value='"+nameArr[n]+"'/></td>"
                    }else if(m==2&&nameArr[n]=="上偏光片"){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value='偏光片TOP'/></td>"
                    }else if(m==2&&nameArr[n]=="下偏光片"){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value='偏光片BOTTOM'/></td>"
                    }else if(m==2&&nameArr[n]=="虚拟偏光片组件"){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value='"+nameArr[n]+"'/></td>"
                    }
                }else{
                    var ceLen=publicText.length;
                    if(m>ceLen||m==ceLen){
                        var getLocalStorageText=getLocalStorage[m-ceLen]!=undefined?getLocalStorage[m-ceLen]:""
                        tableTd+="<td  class='publicAttributes' style='background:#eee; opacity:0.6;filter:Alpha(opacity=60)' title='"+getLocalStorageText+"'>"+getLocalStorageText+"</td>"
                    }else if((m==7||m==3)&&nameArr[n]=="虚拟偏光片组件"){
                        tableTd+="<td><input maxlength='30' readonly='readonly' unselectable='on' type='text' value/></td>"
                    }else{
                        tableTd+="<td><input maxlength='30' type='text' value/></td>"
                    }
                }

            }
            tableTd+="</tr>";
        }
        tableTd+="</tbody>";
        //}
        $(".tabRight").append(tableTd);
        var height=$(".tabRight thead").height();
        $(".tabLeft thead tr").css("height",height+"px");

        var otdHeight=$(".tabRight tbody tr");
        for(var w=0;w<otdHeight.length;w++){
            var offHeight= $(".tabRight tbody tr")[w].clientHeight;
            $(".tabLeft tbody tr").eq(w).css({
                "offsetHeight":otdHeight+"px"
            });
        }
        if(addFlage==undefined||addFlage==""){
        var otrOrSelect=$(".tabRight tbody tr");
        for(var k=0;k<otrOrSelect.length;k++){
            $(".tabRight tbody tr").eq(k).find("td:nth-child(4) input").remove();
            var unit ='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectUnit'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            renderForm(commonObj.CUnit,"Unit"+k,"","","","PCS");
            $(".tabRight tbody tr").eq(k).find("td:nth-child(4)").append(unit);
            var typeName=$(".tabRight tbody tr").eq(k).find("td:nth-child(3) input").val();
            var selectitemThin='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectT'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var ThinningThicknes=commonObj.POLClassType
            renderForm(ThinningThicknes,"T"+k)
            var POLSource='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectTK'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var POLSourceArr=commonObj.POLSource;

            if(typeName=="虚拟偏光片组件"){
                $(".tabRight tbody tr").eq(k).find("td:nth-child(8) input").attr("readonly","readonly")
                $(".tabRight tbody tr").eq(k).find("td:nth-child(9) input").attr("readonly","readonly");
                renderForm(POLSourceArr,"TK"+k,"","","","虚拟件")
                $(".tabRight tbody tr").eq(k).find("td:nth-child(16) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(16)").append(POLSource);
                $(".tabRight tbody tr").eq(k).find("td:nth-child(12) input").attr("readonly","readonly");
                $(".tabRight tbody tr").eq(k).find("td:nth-child(13) input").attr("readonly","readonly");
            }else{
                $(".tabRight tbody tr").eq(k).find("td:nth-child(9) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(9)").append(selectitemThin);
                $(".tabRight tbody tr").eq(k).find("td:nth-child(14) input").attr("readonly","readonly");
                $(".tabRight tbody tr").eq(k).find("td:nth-child(15) input").attr("readonly","readonly");
               // $(".tabRight tbody tr").eq(k).find("td:nth-child(16) input").attr("readonly","readonly");
                $(".tabRight tbody tr").eq(k).find("td:nth-child(17) input").attr("readonly","readonly");
               // $(".tabRight tbody tr").eq(k).find("td:nth-child(18) input").attr("readonly","readonly");
                renderForm(POLSourceArr,"TK"+k,"","","","购买")
                $(".tabRight tbody tr").eq(k).find("td:nth-child(16) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(16)").append(POLSource);

            }
            if(typeName=="上偏光片"||typeName=="下偏光片"){
                var POLisBothNewPolarizerArr=commonObj.POLisBothNewPolarizer;
                var POLisBothNewPolarizer='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectBP'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                renderForm(POLisBothNewPolarizerArr,"BP"+k,"","","","是");
                $(".tabRight tbody tr").eq(k).find("td:nth-child(12) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(12)").append(POLisBothNewPolarizer);
                $(".tabRight tbody tr").eq(k).find("td:nth-child(16) input").attr("readonly","readonly");
            }else{
                $(".tabRight tbody tr").eq(k).find("td:nth-child(12) input").attr("readonly","readonly");
                $(".tabRight tbody tr").eq(k).find("td:nth-child(13) input").attr("readonly","readonly");

            }


            var selectitempartR='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectpartR'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var POLpartRecognize=commonObj.POLpartRecognize;
            renderForm(POLpartRecognize,"partR"+k,"","","","否")
            $(".tabRight tbody tr").eq(k).find("td:nth-child(11) input").remove();
            $(".tabRight tbody tr").eq(k).find("td:nth-child(11)").append(selectitempartR);

        }}else{
            //**********
            var listText=""
            function renderForm2(str,m,selected){
                layui.use('form', function(){
                    var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
                    var option="";
                    var selt=selected!=undefined?selected:"";
                    for(var i=0;i<str.length;i++){
                        if(selt!=""&&selected==str[i]){
                            option+='<option value="" selected>'+str[i]+'</option>'
                        }else{
                            option+='<option value="" >'+str[i]+'</option>'
                        }
                    }
                    listText='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="select'+m+'" name="" lay-verify=""> <option value=""></option>'+option+' </select> </div> </div></form>';
                    form.render("select");
                });
            }
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(4) input").remove();

            renderForm2(commonObj.CUnit,"Unit"+arrAdd,"PCS");
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(4)").append(listText);
            var typeName=$(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(3) input").val();
           // var selectitemThin='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectT'+arrAdd+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';

           // var POLSource='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectTK'+arrAdd+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var POLSourceArr=commonObj.POLSource

            if(typeName=="虚拟偏光片组件"){
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(8) input").attr("readonly","readonly")
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(9) input").attr("readonly","readonly");
                renderForm2(POLSourceArr,"TK"+arrAdd,"虚拟件")
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(18) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(18)").append(listText);
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(12) input").attr("readonly","readonly");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(13) input").attr("readonly","readonly");
            }else{
                var ThinningThicknes=commonObj.POLClassType//偏光片类型
                renderForm2(ThinningThicknes,"T"+arrAdd)
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(9) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(9)").append(listText);
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(14) input").attr("readonly","readonly");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(15) input").attr("readonly","readonly");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(18) input").attr("readonly","readonly");
                //$(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(17) input").attr("readonly","readonly");
                // $(".tabRight tbody tr").find("td:nth-child(20) input").attr("readonly","readonly");
                renderForm2(POLSourceArr,"TK"+arrAdd,"购买")
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(16) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(16)").append(listText);

            }
            if(typeName=="上偏光片"||typeName=="下偏光片"){
                var POLisBothNewPolarizerArr=commonObj.POLisBothNewPolarizer;
                var POLisBothNewPolarizer='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectBP'+arrAdd+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                renderForm2(POLisBothNewPolarizerArr,"BP"+arrAdd,"否");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(12) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(12)").append(POLisBothNewPolarizer);
               // $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(16) input").attr("readonly","readonly");
            }else{
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(12) input").attr("readonly","readonly");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(13) input").attr("readonly","readonly");
            }
           /* if(typeName=="上偏光片"||typeName=="下偏光片"){

            }else{
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(13) input").attr("readonly","readonly");
            }*/


           // var selectitempartR='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectpartR'+arrAdd+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var POLpartRecognize=commonObj.POLpartRecognize;
            renderForm2(POLpartRecognize,"partR"+arrAdd,"否")
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(11) input").remove();
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(11)").append(listText);

        }
        var inputLen=$("tr td");
        for(var h=0;h<inputLen.length;h++){
            if($("tr td ").eq(h).find("input").length>0){
                if($("tr td ").eq(h).find("input").attr("readonly")=="readonly"){
                    $("tr td").eq(h).css({
                        "background":"#eee",
                        "opacity":0.6,
                        "filter":"Alpha(opacity=60)"
                    })
                }
                var inputVal=$("tr td").eq(h).find("input").val();

            }
            if($("tr td ").eq(h).find("select").length>0){
                var selectText=$("tr td").eq(h).find("select option:selected").text();
                $("tr td").eq(h).attr("title",selectText);
            }



        }
    }

    $(".submitMes").click(function(){
        var trLen=$(".tabRight tbody tr");
        var tdLen=$(".tabRight tbody tr:first-child td");
        var dataListObj={
            "number":"",//物料编号
            "name":"",//物料名称
            "createType":"",//类型:
            "defaultUnit":"",//默认单位
            "partCName":"",//物料中文名称
            "partDrawingNumber":'',//材料型号/图纸编号:
            "partMainSpecification":"",//主要规格（长×宽×厚mm）
            "absorptionShaftAngle":"",//吸收轴角度(与长边逆时针方向夹角，注：长边为靠近source IC侧的边)：
            "polarizerType":"",//偏光片类型
            "surfaceTreatment":"",//表面处理方式：
            "partRecognize":"",//是否需要零件承认（POL、背光、铁框、TP、PCB（A）、FPC（A）需要零件承认）
           /* "MtlNumofPOLBtn":"",//对应下POL料号(若已有料号，务必填写正确，涉及关联mark):
            "MtlNumofPOLTop":"",//对应上POL料号(若已有料号，务必填写正确，涉及关联mark):*/
            "isBothNewPolarizer":"",//上下偏光片是否均为新增(务必确认上下偏一同申请还是仅单侧申请):
             "partDrawingNumber1":"",//上/下偏光片料号（格式例如：上偏：1040008530，下偏：1040008540）:
            "POLResolution":"",//分辨率（长×宽）:
            "DisplayMode":"",//显示模式:
          /*  "MtlNumofPOLTop1":"",//对应上POL料号:"
            "MtlNumofPOLBtn1":"",//   对应下POL料号:*/
            "source":"",//源
            "partEnvProtectionCategory":"",//环保类别
            "finishedGoodType":"",//产品型号
            "partNumberEffectiveSite":"",//料号产地
            "partInspectionStandard":"",//检验标准:
            "partSupplier":"",//供应商：
            "materialClassification":"",//物料分级
            "partRFQNumber":"",//RFQ编号（深圳用）
            "specialMonitor":"",//是否专显
            "partApplicationReason":"",//申请人及申请原因:
            "partNote":"",//备注说明：
            "partSize":""//尺寸(模组尺寸，例如：5.13、4.30等)
        };

        var datalist=[];
        for(var i=0;i<trLen.length;i++){
            var dataArr=[];
            for(var m=0;m<tdLen.length;m++){
                if(m>15){
                    var oTdText=$(trLen).eq(i).find("td").eq(m).text();
                    dataArr.push(oTdText)
                }else{
                    var input = $(trLen).eq(i).find("td").eq(m).find("input").val();
                    if(input!=undefined){
                        if(input=="<自动生成>"){
                            input="";
                        }

                    }else{
                        input=$(trLen).eq(i).find("td").eq(m).find("select option:selected").text();
                    }
                    if((i==3||i==15)&&input==""){
                        layerFn("请将带*的所有属性填写完整");
                        return;
                    }
                    dataArr.push(input);
                }
            }
            var dataV=[];
            for(var v in dataListObj){
                dataV.push(v)
            }
            var obj={"roottype":"Material"};
            for(var n=0;n<dataV.length;n++){
                obj[dataV[n]]=dataArr[n];

            }
            obj["roottype"]="Material";
            //console.log(dataV);
            datalist.push(obj);
        }
        var url="/MaterialsApplication/tmCreateMaterialsApplication";
        var data={
            datalist:datalist
        }
        $("#loading").css({
            display:"block"
        });
        ajaxNewFn(url,data,function(str){
            $("#loading").css({
                display:"none"
            });
            if(str.numberlist!=undefined&&str.numberlist.length>0){
                var num="";
                for(var k=0;k<str.numberlist.length;k++){
                    if(k!=(str.numberlist.length-1)){
                        num+=str.numberlist[k]+'；'
                    }else{
                        num+=str.numberlist[k]
                    }

                }
                var mesg="物料"+num+"在配套物料申请编号"+str.econumber+"中创建成功"
                layerFn(mesg,function(){
                    window.location.href="about:blank";
                    window.opener=null;
                    window.open('','_self');
                    window.close();
                })
            }else{
                var mesg="物料在配套物料申请编号"+str.econumber+"创建失败"
                layerFn(mesg)
            }
        })

    })
}